/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#pragma once
#include <osgEarth/Common>
#include <osgEarth/TileKey>
#include <osgEarth/VisibleLayer>
#include <osgEarth/TextureArena>
#include <osgEarth/StateSetCache>
#include <osgEarth/Chonk>
#include <osg/Node>

namespace osgEarth
{
    class ProgressCallback;

    /**
     * Layer that creates a scene graph from a tiled data source.
     * This class is abstract; subclasses must implement createTileImplementation().
     */
    class OSGEARTH_EXPORT TiledModelLayer : public VisibleLayer
    {
    public:
        // options serialization
        struct OSGEARTH_EXPORT Options : public VisibleLayer::Options
        {
            META_LayerOptions(osgEarth, Options, VisibleLayer::Options);
            OE_OPTION(bool, additive);
            OE_OPTION(float, rangeFactor);
            OE_OPTION(unsigned, minLevel, 0u);
            OE_OPTION(unsigned, maxLevel, 99u);
            OE_OPTION(bool, nvgl, false);
            virtual Config getConfig() const;
            void fromConfig(const Config& conf);
        };

    public:
        META_Layer_Abstract(osgEarth, TiledModelLayer, Options, VisibleLayer);

        //! Create an OSG node from a tile key.
        //! @param key The tile key.
        //! @param  progress A progress callback
        osg::ref_ptr<osg::Node> createTile(const TileKey& key, ProgressCallback* progress) const;

        //! Profile of the underlying tiled data source.
        virtual const Profile* getProfile() const = 0;

        //! Minimum available LOD of the underlying tiled data source.
        virtual unsigned getMinLevel() const;
        void setMinLevel(unsigned value);

        //! Maximum available LOD of the underlying tiled data source.
        virtual unsigned getMaxLevel() const;
        void setMaxLevel(unsigned value);

        //! Whether new LODs should be added to the scene (true) or replace existing ones (false)
        void setAdditive(const bool& value);
        const bool& getAdditive() const;

        //! The range factor.  Default is 6
        void setRangeFactor(const float &value);
        const float& getRangeFactor() const;

        //! Forces a rebuild on this layer.
        void dirty();

    public: // Layer

        // The Node representing this layer.
        osg::Node* getNode() const override;

        // called by the map when this layer is added
        void addedToMap(const Map*) override;

        // called by the map when this layer is removed
        void removedFromMap(const Map*) override;

        // post-ctor initialization
        void init() override;

    protected:
        //! Subclasses must implement this to create a tile.
        //! @param key The tile key, in the underlying tiled data source's profile.
        //! @param progress A progress callback that can be checked for user cancellation.
        virtual osg::ref_ptr<osg::Node> createTileImplementation(const TileKey& key, ProgressCallback* progress) const = 0;

        // NVGL support:
        osg::ref_ptr<TextureArena> _textures;
        mutable ChonkFactory _chonkFactory;

    private:
        osg::observer_ptr< const Map > _map;
        osg::ref_ptr< osg::Group > _root;
        bool _graphDirty;
        osg::ref_ptr<StateSetCache> _statesetCache;
        void create();
    };
}
